package com.me.bbs.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.me.bbs.anno.RequireAdmin;
import com.me.bbs.anno.RequireLogin;
import com.me.bbs.common.AdminType;
import com.me.bbs.common.AuthUtils;
import com.me.bbs.common.Resp;
import com.me.bbs.common.SessionUtils;
import com.me.bbs.dto.ForumDTO;
import com.me.bbs.entity.Forum;
import com.me.bbs.entity.ForumCategory;
import com.me.bbs.entity.Post;
import com.me.bbs.service.IForumCategoryService;
import com.me.bbs.service.IForumService;
import org.jetbrains.annotations.NotNull;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lm
 * @since 2023-01-11
 */
@RestController
@RequestMapping("/forum")
public class ForumController {
    @Resource
    private IForumService forumService;
    @Resource
    private IForumCategoryService forumCategoryService;
    @PostMapping("/list")
    public Resp<?> getForumsForList(@RequestBody Map<String, Object> map) {

        long current = ((Integer)map.get("current")).longValue();
        long size = ((Integer)map.get("size")).longValue();
        Page<Forum> forumIPage = new Page<>(current, size);
        LambdaQueryWrapper<Forum> wrapper = new LambdaQueryWrapper<>();
        Long category = null;
        if(map.get("category") != null)
            category = Long.parseLong((String) map.get("category"));
        String kw =  (String) map.get("kw");
        wrapper.eq(category!=null, Forum::getCategory, category)
                .like(StringUtils.hasLength(kw),Forum::getForumName,kw)
                .orderByDesc(Forum::getSubscribe)
                .orderByDesc(Forum::getPostCount)
                        .orderByAsc(Forum::getId);
      forumService.page(forumIPage, wrapper);
        return Resp.success(forumIPage, "成功");
    }
    @GetMapping("/categories")
    public Resp<?> getAll() {
        List<ForumCategory> list = forumCategoryService.list();
        return Resp.success(list,"成功");
    }

    @PostMapping("/hot")
    public Resp<?> getHot() {
        List<Forum> list = forumService.lambdaQuery().orderByDesc(Forum::getPostCount)
                .orderByDesc(Forum::getSubscribe)
                .last("limit 10")
                .list();
        return Resp.success(list, "成功");
    }

    @GetMapping("/{id}")
    public Resp<?> getForumInfo(@PathVariable Long id) {

        ForumDTO forumDetail = forumService.getForumDetail(id);
        return Resp.success(forumDetail,"成功");
    }

    @PostMapping("/create")
    @RequireLogin
    public Resp<?> createForum(@RequestBody Forum forum) {
        Long userId = (Long) SessionUtils.getAttribute("user");
        forum.setOwner(userId);
        forumService.save(forum);
        return Resp.success(forum.getId(),"成功");
    }

    @GetMapping("/statistics/{id}")
    public Resp<?> getTodayStatistics(@PathVariable("id") Long forumId) {
        Map<String, Object> todayStatistics = forumService.getTodayStatistics(forumId);
        return  Resp.success(todayStatistics, "成功");
    }

    @GetMapping("/statistics/{id}/week")
    public Resp<?> getWeekStatistics(@PathVariable("id") Long forumId) {
        Map<String, Object> todayStatistics = forumService.getWeekStatistics(forumId);
        return  Resp.success(todayStatistics, "成功");
    }

    @PutMapping("/save")
    public Resp<?> saveForum(@RequestParam("forumId") @NotNull Long forumId,
                             @RequestParam(value = "avatar", required = false) String  avatar,
                             @RequestParam(value = "intro", required = false) String  intro,
                             @RequestParam(value = "bgImg", required = false) String  bgImg) {

        AuthUtils.checkAdmin(AdminType.OWNER, forumId);
        Forum forum = new Forum();
        forum.setIntro(intro);
        forum.setAvatar(avatar);
        forum.setBgImg(bgImg);
        forum.setId(forumId);
        boolean update = forumService.lambdaUpdate().eq(Forum::getId, forumId)
                .set(avatar != null, Forum::getAvatar, avatar)
                .set(intro != null, Forum::getIntro, intro)
                .set(bgImg != null, Forum::getBgImg, bgImg)
                .update();
        if(update)
            return Resp.success();
        return Resp.error();
    }


}
